using UnityEngine;

namespace HyuFrameWork
{
    //帮助播放音乐 为了简写代码
    public class AudioHelper
    {
        // 播放一下
        public static AudioSource PlayOneShot(AudioSource src, AudioClip clip, bool interrupt = true) 
        {
            if (!interrupt && src.isPlaying) 
                return src;
            src.PlayOneShot(clip);
            return src;
        }
        
        // 立刻切播(如果用的是同一个clip，会从头开始播)
        public static AudioSource PlayInstant(AudioSource src, AudioClip clip, bool loop = false) 
        {
            src.clip = clip;
            src.loop = loop;
            src.Play();
            return src;
        }
        
        // 从中间开始播放音乐
        public static AudioSource PlayAtTime(AudioSource src, AudioClip clip, float time) 
        {
            time = Mathf.Clamp01(time);
            time *= clip.length;
            
            src.clip = clip;
            src.time = time;
            src.Play();
            return src;
        }
    }
}
